//
//  UIView+TapBlock.m
//  XiuXiuTuanGou
//
//  Created by 张敬 on 2019/5/8.
//  Copyright © 2019年 XiuXiuTuanGou. All rights reserved.
//

#import "UIView+TapBlock.h"

static char const * const ObjectTagKey = "ObjectTag";

@implementation UIView (TapBlock)


#pragma mark - getter methods

- (dealGeneralOperation)tapBlock
{
    return objc_getAssociatedObject(self, @selector(tapBlock));
}

- (BOOL)isNeedAnimation{
    
    NSNumber *number = objc_getAssociatedObject(self, ObjectTagKey);;
    return  [number boolValue];
}

- (UIViewController *)viewController {
    for (UIView *view = self; view; view = view.superview) {
        UIResponder *nextResponder = [view nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}


#pragma mark - setter methods

- (void)setTapBlock:(dealGeneralOperation)tapBlock
{
    objc_setAssociatedObject(self, @selector(tapBlock), tapBlock, OBJC_ASSOCIATION_COPY);
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dealTapOperation)];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:tap];
}

- (void)setIsNeedAnimation:(BOOL)isNeedAnimation
{
    NSNumber *number = [NSNumber numberWithBool: isNeedAnimation];
    objc_setAssociatedObject(self, ObjectTagKey, number , OBJC_ASSOCIATION_ASSIGN);
}


- (void)dealTapOperation{
    
    if (self.isNeedAnimation) {
        CABasicAnimation *animation = [CABasicAnimation makeScaleBigAndSamllAnimation];
        [self.layer addAnimation:animation forKey:nil];
    }
    
    if (self.tapBlock) {
        self.tapBlock();
    }
}

@end
